<?

class Users
{
	function Users() {
		global $configuration;
		$this->service = $configuration->get("service");
	}
	function newUser($name, $pwd, $email) {
		global $db;
		$data = $db->queryToArray("SELECT * FROM", "users", "WHERE name='".quote_smart($name)."'");
		if (!is_array($data)) {
			$db->query("INSERT INTO", "users", "(name, password, email) VALUES  ('".quote_smart($name)."', '".quote_smart($pwd)."', '".quote_smart($email)."')");
			return 1;
		} else {
			return 0;
		}
	}
	function logUserIn($name, $pwd) {
		global $db;
		$data = $db->queryToArray("SELECT * FROM", "users", "WHERE name='".quote_smart($name)."'");
		if (is_array($data)) {
			if ($data[0]["password"] == quote_smart($pwd)) {
				$a["userName"] = $data[0]["name"];
				$a["user"] = $data[0]["ID"];
				$a["loged_in"] = true;
				return $a;
			} else return 0;
		} else {
			return 0;
		}
	}
	function getUserName($id) {
		global $db;
		$data = $db->queryToArray("SELECT * FROM", "users", "WHERE ID = '".$id."'");
		if (is_array($data)) {
			return $data[0]["name"];
		} else {
			return false;
		}
	}
	function getUserData($id) {
		global $db;
		$data = $db->queryToArray("SELECT * FROM", "users", "WHERE ID = '".$id."'");
		if (is_array($data)) {
			return $data[0];
		} 
		return false;
	}
	function getUserGroups($id) {
		global $db;
		$data = $db->queryToArray("SELECT * FROM", "user_groups", "WHERE ID = '".$id."'");
		if (is_array($data)) {
			$groups = array();
			foreach ($data as $d)
				$groups[] = $d["grp"];
			return $groups;
		}
		return array();
	}
	function getAllUsers() {
		global $db;
		$data = $db->queryToArray("SELECT * FROM", "users", " ORDER BY name");
		if (is_array($data)) {
			return $data;
		} else {
			return false;
		}
	}
	function getAllUsersByEmail($mail) {
		global $db;
		$data = $db->queryToArray("SELECT * FROM", "users", " WHERE email LIKE '%".$mail."%' ORDER BY name");
		if (is_array($data)) {
			return $data;
		} else {
			return false;
		}
	}
	function getUserByName($n) {
		global $db;
		$data = $db->queryToArray("SELECT * FROM", "users", "WHERE name = '".$n."'");
		if (is_array($data)) {
			return $data[0]["ID"];
		} else {
			return false;
		}
	}
	function getUserPermission($id) {
		global $db;
		$data = $db->queryToArray("SELECT * FROM", "users", "WHERE ID = '".$id."'");
		if (is_array($data)) {
			return $data[0]["permission"];
		} else {
			return false;
		}
	}
	function setUserPermission($id, $per) {
		global $db;
		$db->query("UPDATE ", "users", " SET permission = '".$per."' WHERE ID = '".$id."'");
	}
	function setUserName($id, $n) {
		global $db;
		$db->query("UPDATE ", "users", " SET name = '".$n."' WHERE ID = '".$id."'");
	}
	function setUserPwd($id, $pwd) {
		global $db;
		$db->query("UPDATE ", "users", " SET password = '".$pwd."' WHERE ID = '".$id."'");
	}
	function setUserEmail($id, $e) {
		global $db;
		$db->query("UPDATE ", "users", " SET email = '".$e."' WHERE ID = '".$id."'");
	}
	function getUserAttributes($id) {
		global $db;
		$Attributes = $db->queryToAssocArray("SELECT * FROM", "user_attributes", "WHERE ID='".$id."'", "name");
		return $Attributes;
	}
	function getUserAttribute($id, $name, $default = false) {
		global $db;
		$data = $db->queryToArray("SELECT * FROM", "user_attributes", "WHERE ID = '".$id."' AND name='".$name."'");
		if (is_array($data)) {
			return $data[0]["value"];
		} else {
			return $default;
		}
	}
	function setUserAttribute($id, $name, $value) {
		global $db;
		$db->query("DELETE FROM", "user_attributes", "WHERE name='".$name."' AND ID='".$id."'");
		$db->query("INSERT INTO", "user_attributes", "(ID, name, value) VALUES ('".$id."', '".$name."', '".$value."')");
	}
	function setUserGroup($id, $group) {
		global $db;
		$db->query("DELETE FROM", "user_groups", "WHERE grp='".$group."' AND ID='".$id."'");
		$db->query("INSERT INTO", "user_groups", "(ID, grp) VALUES ('".$id."', '".$group."')");
	}
	function deleteUserGroup($id, $group) {
		global $db;
		$db->query("DELETE FROM", "user_groups", "WHERE grp='".$group."' AND ID='".$id."'");
	}
}

$users = new Users();

?>